package cfca.seal.front.service.servlet;

import cfca.sadk.util.Base64;
import cfca.seal.front.service.FunctionType;
import cfca.seal.front.service.ws.IWebSealService;
import cfca.seal.util.StringUtil;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class WebSealServlet extends HttpServlet
{
  private static final long serialVersionUID = -2802334706155081655L;
  private static final Logger logger = LoggerFactory.getLogger("service_sys");

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("UTF-8");
    String type = request.getParameter("type");
    OutputStream os = response.getOutputStream();
    try {
      ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
      IWebSealService webSealService = (IWebSealService)applicationContext.getBean("webSealServiceImpl");

      if (type.equals(FunctionType.signWebSeal.toString()))
      {
        String srcBase64 = request.getParameter("sourceBase64");
        if (StringUtil.isNotEmpty(srcBase64)) {
          srcBase64 = new String(Base64.decode(srcBase64.getBytes("UTF-8")), "UTF-8");
        }
        String strategyXml = request.getParameter("sealStrategyXml");
        if (StringUtil.isNotEmpty(strategyXml)) {
          strategyXml = new String(Base64.decode(strategyXml.getBytes("UTF-8")), "UTF-8");
        }
        byte[] signWebSeal = webSealService.signWebSeal(srcBase64, strategyXml);
        byte[] result = Base64.encode(signWebSeal);
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.verifyWebSeal.toString()))
      {
        String webSealResult = request.getParameter("webSealSource");
        if (StringUtil.isNotEmpty(webSealResult)) {
          webSealResult = new String(Base64.decode(webSealResult.getBytes("UTF-8")), "UTF-8");
        }
        String srcBase64 = request.getParameter("sourceBase64");
        if (StringUtil.isNotEmpty(srcBase64)) {
          srcBase64 = new String(Base64.decode(srcBase64.getBytes("UTF-8")), "UTF-8");
        }
        String verifyStrategyXML = request.getParameter("verifyStrategyXML");
        if (StringUtil.isNotEmpty(verifyStrategyXML)) {
          verifyStrategyXML = new String(Base64.decode(verifyStrategyXML.getBytes("UTF-8")), "UTF-8");
        }
        String verifyWebSeal = webSealService.verifyWebSeal(webSealResult, srcBase64, verifyStrategyXML);
        byte[] result = Base64.encode(verifyWebSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      }

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    catch (Exception e)
    {
      logger.error("C0001", e);
      StringBuilder result = new StringBuilder();
      result.append("<Error><errorCode>");
      result.append("C0001");
      result.append("</errorCode><ErrorMessage>");
      result.append("错误");
      result.append("</ErrorMessage>");
      byte[] returnValue = Base64.encode(result.toString().getBytes("UTF-8"));
      os.write(returnValue, 0, returnValue.length);

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    finally
    {
      if (null != os) {
        os.flush();
        os.close();
      }
    }
  }
}